package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.EnterpriseWeChat.entity.OpenCardTask;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 开卡任务
 */
public class GetOpenCardTaskData extends RESTBase {
    private final static String RANGE = "10";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetOpenCardTaskData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public OpenCardTask getData(String empId, String storeId, String month) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,storeId,month), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        if(jsonArray.length()>0){
            OpenCardTask openCardTask = new OpenCardTask();
            openCardTask.setStoreTask(jsonArray.getJSONArray(0).getString(0));
            openCardTask.setStoreCompleted(jsonArray.getJSONArray(0).getString(1));
            openCardTask.setStoreRank(jsonArray.getJSONArray(0).getString(2));
            openCardTask.setEmpTask(jsonArray.getJSONArray(0).getString(3));
            openCardTask.setEmpCompleted(jsonArray.getJSONArray(0).getString(4));
            openCardTask.setEmpRank(jsonArray.getJSONArray(0).getString(5));
            return openCardTask;

        }

        return null;
    }

    private MultiValueMap Data(String empId,String storeId,String month) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'C_STOREODMARK_ID;TOT_AMT_MARK'");//店仓月开卡指标
        colums.add("'C_STOREODMARK_ID;TOT_OPENVIP'");//店仓本月开卡数
        colums.add("'C_STOREODMARK_ID;COM_RATE'");//店仓达成率
        colums.add("'TOT_AMT_MARK'");//员工开卡指标
        colums.add("'TOT_OPENVIP'");//员工完成数
        colums.add("'COM_RATE'");//个人达成率

        params.put("columns", colums);
        params.put("table", "C_STOREODMARKITEM");//表名
//        params.put("range",RANGE);
////        params.put("count","true");
//        int r = Integer.valueOf(RANGE);
//        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'HR_EMPLOYEE_ID'");
        String ac = "'"+empId+"'";
        expr1.put("condition",ac);

        HashMap expr2 = new HashMap();
        expr2.put("column","'C_STOREODMARK_ID;C_STORE_ID'");
        String state= "'"+storeId+"'";
        expr2.put("condition",state);

        HashMap expr4 = new HashMap();
        HashMap expr3 = new HashMap();
        expr3.put("column","'C_STOREODMARK_ID;YEARMONTH'");
        String c = "'"+month+"'";
        expr3.put("condition",c);

        expr4.put("combine", "and");
        expr4.put("expr1", expr1);
        expr4.put("expr2", expr2);

        paramss.put("combine", "and");
        paramss.put("expr1", expr4);
        paramss.put("expr2", expr3);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetOpenCardTaskData g = new GetOpenCardTaskData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("","","202102"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
